package com.ziyun.erp.web.modules.quoteAttribute.controller;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeOptionEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeOptionService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 报价属性选项表--材料属性
 *
 * @author jiangli
 * @email
 * @date 2018-09-06 08:52:31
 */
@RestController
@RequestMapping("quoteAttribute/quoteattributeoption")
public class QuoteAttributeOptionController {
    @Autowired
    private QuoteAttributeOptionService quoteAttributeOptionService;

    /**
     * 列表
     */
    @RequestMapping("/list/{id}")
    @RequiresPermissions("quoteAttribute:quoteattributeoption:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Integer id) {
        params.put("id", id);
        PageUtils page = quoteAttributeOptionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteAttribute:quoteattributeoption:info")
    public R info(@PathVariable("id") Integer id) {
        QuoteAttributeOptionEntity quoteAttributeOption = quoteAttributeOptionService.selectById(id);
        List<Map<String, Object>> gramdepths = quoteAttributeOptionService.getGramdepthsByOptionName(quoteAttributeOption.getOptionName());
        return R.ok().put("quoteAttributeOption", quoteAttributeOption).put("gramdepths", gramdepths);
    }

    /**
     * 添加
     */
    @RequestMapping("/save")
    @RequiresPermissions("quoteAttribute:quoteattributeoption:save")
    public R save(@RequestBody QuoteAttributeOptionEntity quoteAttributeOption) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        quoteAttributeOption.setCreateUser(curentUser.getUsername());
        quoteAttributeOptionService.insertQuoteAttributeOptionEntity(quoteAttributeOption);
        return R.ok();
    }

    /**
     * 添加
     */
    @RequestMapping("/saves")
    @RequiresPermissions("quoteAttribute:quoteattributeoption:save")
    public R save(@RequestBody List<QuoteAttributeOptionEntity> quoteAttributeOptions) {
        //查询选项名称是否重复
        int count = quoteAttributeOptionService.getOptionNameCount(quoteAttributeOptions.get(0).getOptionName());
        if (count > 0) {
            return R.error("选项名称已存在");
        }
        for (QuoteAttributeOptionEntity quoteAttributeOption : quoteAttributeOptions) {
            SysUserEntity curentUser = UserUtils.getCurentUser();
            quoteAttributeOption.setCreateUser(curentUser.getUsername());
            quoteAttributeOptionService.insertQuoteAttributeOptionEntitys(quoteAttributeOption);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteAttribute:quoteattributeoption:update")
    public R update(@RequestBody List<QuoteAttributeOptionEntity> quoteAttributeOptions) {
        return quoteAttributeOptionService.updateList(quoteAttributeOptions);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteAttribute:quoteattributeoption:delete")
    public R delete(@RequestBody Integer[] ids) {
        return quoteAttributeOptionService.deleteByIds(ids);
    }

    /**
     * 得到材料选项集合
     */
    @GetMapping("/getMaterialList")
    public List<QuoteAttributeOptionEntity> getMaterialList() {
        return quoteAttributeOptionService.getMaterialList();
    }

    //删除材料
    @RequestMapping("/deleteAll/{optionName}")
    public R deleteAll(@PathVariable String optionName) {
        return quoteAttributeOptionService.deleteAll(optionName);
    }

}
